package com.byt.base.activity;

import android.os.Bundle;
import android.view.View;

import com.byt.base.AppManager;
import com.byt.base.R;
import com.byt.base.loadsir.EmptyCallback;
import com.byt.base.loadsir.ErrorCallback;
import com.byt.base.loadsir.LoadingCallback;
import com.byt.base.utils.ToastUitl;
import com.byt.base.viewmodel.BaseViewModel;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

public abstract class BasePageActivity<V extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity implements IPageBaseView {
    protected  VM viewModel;
    protected  V dataBinding;
    private LoadService mLoadService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDataBefore();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (viewModel != null && viewModel.isUIAttached()) {
            viewModel.detachUI();
        }
        AppManager.getAppManager().removeActivity(this);
    }

    @Override
    public void showEmpty() {
        if(mLoadService != null) {
            mLoadService.showCallback(EmptyCallback.class);
        }
    }

    @Override
    public void onRefreshFailure(String message) {
        if(mLoadService != null) {
            mLoadService.showCallback(ErrorCallback.class);
        }
    }

    @Override
    public void showLoading() {
        if (mLoadService != null) {
            mLoadService.showCallback(LoadingCallback.class);
        }
    }

    @Override
    public void onLoadMoreFail(String message) {
        ToastUitl.showShort(message);
    }

    @Override
    public void onLoadMoreEmpty() {
        ToastUitl.showShort(getString(R.string.no_more_data));
    }

    @Override
    public void showContent() {
        if (mLoadService != null) {
            mLoadService.showSuccess();
        }
    }

    public void setLoadSir(View view) {
        // You can change the callback on sub thread directly.
        mLoadService = LoadSir.getDefault().register(view, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                onRetryBtnClick();
            }
        });
    }

    // 点击事件
    protected abstract void onRetryBtnClick();

    // 获取资源文件
    protected abstract
    @LayoutRes
    int getLayoutId();

    // 获取ViewModel
    protected abstract VM getViewModel();

    // 获取数据绑定参数
    public abstract int getBindingVariable();

    private void initDataBefore(){
        AppManager.getAppManager().addActivity(this);
        dataBinding = DataBindingUtil.setContentView(this,getLayoutId());
        if (viewModel == null){
            viewModel = getViewModel();
        }
        // 如果有数据变量完成数据绑定
        if(getBindingVariable() > 0) {
            dataBinding.setVariable(getBindingVariable(), viewModel);
        }
        dataBinding.executePendingBindings();
    }
}

